﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PhatPhucLoc.model;
using System.Data;
using System.Data.SqlClient;
using PhatPhucLoc.util;

namespace PhatPhucLoc.dao
{
    class KhachHangDAO
    {
        public static KhachHangModel GetByID(String _ID)
        {
            KhachHangModel _model = null;
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("ID", SqlDbType.Int);
            param[0].Value = _ID;

            DataTable _dt = DataUtil.executeStore("sp_KhachHang_GetByID", param);

            try
            {
                foreach (DataRow dr in _dt.Rows)
                {
                    _model = new KhachHangModel();
                    _model.Id = dr["ID"].ToString();
                    _model.Ten = dr["Ten"].ToString();
                    _model.DiaChi = dr["Dia_chi"].ToString();
                    _model.Tel = dr["Tel"].ToString();
                    _model.Fax = dr["Fax"].ToString();
                    _model.Email = dr["Email"].ToString();
                    _model.MaSoThue = dr["Ma_so_thue"].ToString();
                    _model.GhiChu = dr["Ghi_chu"].ToString();
                    _model.SoTaiKhoan = dr["So_tai_khoan"].ToString();
                    _model.NganHang = dr["Ngan_hang"].ToString();
                    _model.NgayCapNhatCuoi = dr["Ngay_cap_nhap_cuoi"].ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
            return _model;
        }

        /******/
        public static DataTable KhachHang_GetAll()
        {
            return DataUtil.executeStore("sp_KhachHang_GetAll", null);
        }

        public static DataTable KhachHangMuaHang_GetByMaKhuVuc(String _MaKhuVuc)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("Ma_khu_vuc", SqlDbType.Int);
            param[0].Value = _MaKhuVuc;

            return DataUtil.executeStore("sp_KhachHang_KhachHangMuaHang_GetByMaKhuVuc", param);
        }

        public static DataTable KhachHangMuaHang_SearchByMaKhuVuc_Ten(String _MaKhuVuc, String _Keyword)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("Ma_khu_vuc", SqlDbType.Int);
            param[0].Value = _MaKhuVuc;
            param[0] = new SqlParameter("Keyword", SqlDbType.NVarChar);
            param[0].Value = _Keyword;

            return DataUtil.executeStore("sp_KhachHang_KhachHangMuaHang_SearchByMaKhuVuc_Ten", param);
        }

        public static DataTable KhachHangMuaHang_SearchByName(String _Keyword)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("Keyword", SqlDbType.NVarChar);
            param[0].Value = _Keyword;

            return DataUtil.executeStore("sp_KhachHang_KhachHangMuaHang_SearchByName", param);
        }


        /******/
        public static DataTable NhaCungCap_GetAll()
        {
            return DataUtil.executeStore("sp_KhachHang_NhaCungCap_GetAll", null);
        }

        public static DataTable NhaCungCap_GetByMaKhuVuc(String _MaKhuVuc)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("Ma_khu_vuc", SqlDbType.Int);
            param[0].Value = _MaKhuVuc;

            return DataUtil.executeStore("sp_NhaCungCap_KhachHangMuaHang_GetByMaKhuVuc", param);
        }

        public static DataTable NhaCungCap_SearchByMaKhuVuc_Ten(String _MaKhuVuc, String _Keyword)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("Ma_khu_vuc", SqlDbType.Int);
            param[0].Value = _MaKhuVuc;
            param[0] = new SqlParameter("Keyword", SqlDbType.NVarChar);
            param[0].Value = _Keyword;

            return DataUtil.executeStore("sp_NhaCungCap_KhachHangMuaHang_SearchByMaKhuVuc_Ten", param);
        }

        public static DataTable NhaCungCap_SearchByName(String _Keyword)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("Keyword", SqlDbType.NVarChar);
            param[0].Value = _Keyword;

            return DataUtil.executeStore("sp_NhaCungCap_KhachHangMuaHang_SearchByName", param);
        }
        public static DataTable NhaCungCap_GetAllNhomNcc()
        {
            return DataUtil.executeStore("sp_NhomNhaCungCap_GetAll", null);
        }
        /******/
        public static Boolean Insert(KhachHangModel _model)
        {
            SqlParameter[] param = new SqlParameter[9];
            int i = 0;
            param[i] = new SqlParameter("Ten", SqlDbType.NVarChar);
            param[i++].Value = _model.Ten;
            param[i] = new SqlParameter("Dia_chi", SqlDbType.NVarChar);
            param[i++].Value = _model.DiaChi;
            param[i] = new SqlParameter("Tel", SqlDbType.NVarChar);
            param[i++].Value = _model.Tel;
            param[i] = new SqlParameter("Fax", SqlDbType.NVarChar);
            param[i++].Value = _model.Fax;
            param[i] = new SqlParameter("Email", SqlDbType.NVarChar);
            param[i++].Value = _model.Email;
            param[i] = new SqlParameter("So_tai_khoan", SqlDbType.NVarChar);
            param[i++].Value = _model.SoTaiKhoan;
            param[i] = new SqlParameter("Ngan_hang", SqlDbType.NVarChar);
            param[i++].Value = _model.NganHang;
            param[i] = new SqlParameter("Ma_so_thue", SqlDbType.NVarChar);
            param[i++].Value = _model.MaSoThue;
            param[i] = new SqlParameter("Ghi_chu", SqlDbType.NVarChar);
            param[i++].Value = _model.GhiChu;
           // param[i] = new SqlParameter("Ngay_cap_nhat_cuoi", SqlDbType.VarChar);
           // param[i++].Value = _model.NgayCapNhatCuoi;
            return DataUtil.executeNonStore("sp_KhachHang_Insert", param);
        }

        public static Boolean Update(KhachHangModel _model)
        {
            SqlParameter[] param = new SqlParameter[10];
            int i = 0;
            param[i] = new SqlParameter("ID", SqlDbType.NVarChar);
            param[i++].Value = _model.Id;
            param[i] = new SqlParameter("Ten", SqlDbType.NVarChar);
            param[i++].Value = _model.Ten;
            param[i] = new SqlParameter("Dia_chi", SqlDbType.NVarChar);
            param[i++].Value = _model.DiaChi;
            param[i] = new SqlParameter("Tel", SqlDbType.NVarChar);
            param[i++].Value = _model.Tel;
            param[i] = new SqlParameter("Fax", SqlDbType.NVarChar);
            param[i++].Value = _model.Fax;
            param[i] = new SqlParameter("Email", SqlDbType.NVarChar);
            param[i++].Value = _model.Email;
            param[i] = new SqlParameter("So_tai_khoan", SqlDbType.NVarChar);
            param[i++].Value = _model.SoTaiKhoan;
            param[i] = new SqlParameter("Ngan_hang", SqlDbType.NVarChar);
            param[i++].Value = _model.NganHang;
            param[i] = new SqlParameter("Ma_so_thue", SqlDbType.NVarChar);
            param[i++].Value = _model.MaSoThue;
            param[i] = new SqlParameter("Ghi_chu", SqlDbType.VarChar);
            param[i++].Value = _model.GhiChu;
            //param[i] = new SqlParameter("Ngay_cap_nhat_cuoi", SqlDbType.VarChar);
            //param[i++].Value = _model.NgayCapNhatCuoi;
            return DataUtil.executeNonStore("sp_KhachHang_Update", param);
        }
    }
}
